﻿using System;


using System.Text;
using IAM.Interface.Model.Interface;
using System.Xml;
using System.Reflection;

namespace IAM.Interface.Model
{
    /// <summary>
    /// 所有权限模型定义
    /// </summary>
    public class ACMMode : AbstractConvertObject, IConvertXml
    {
        #region 属性
        private ACSections _sectionList = null;
        private Dimensions _dimensionList = null;
        private bool _multisection = false;
        /// <summary>
        /// 本节包含权限模型中权限区定义列表
        /// 包含1个或多个权限区的描述
        /// </summary>
        public ACSections SectionList
        {
            get
            {
                return _sectionList;
            }
            set
            {
                _sectionList = value;
            }
        }
        /// <summary>
        /// 权限模型中所有权限维度的列表
        /// 包含1个或多个权限维度Dimension
        /// </summary>
        public Dimensions DimensionList
        {
            get
            {
                return _dimensionList;
            }
            set
            {
                _dimensionList = value;
            }
        }
        /// <summary>
        /// 描述权限模型是否存在多个权限区
        /// </summary>
        public bool MultiSection
        {
            get
            {
                return _multisection;
            }
            set
            {
                _multisection = value;
            }
        }
        #endregion
        #region IConvertXml

        public string XMLBoday
        {
            get
            {
                StringBuilder _xml = new StringBuilder();
                //1.添加 ACMMode 的attributes
                _xml.Append("<ACMMode");
                _xml.Append(" MultiSection=\"" + this.MultiSection + "\"");
                _xml.Append(" >");
                //2.添加SectionList的xml
                _xml.Append(this.SectionList.XML);
                //3.添加DimensionListxml
                _xml.Append(this.DimensionList.XML);

                return _xml.ToString();
            }


        }

        public string XMLFinis
        {
            get
            {
                return "</ACMMode>";
            }
        }
        public string XML
        {
            get
            {
                return XMLBoday + XMLFinis;
            }
        }
        #endregion
        #region AbstractConvertObject
        public override object ConvertToObject()
        {
            if (this.CurrentXmlNode.Attributes["MultiSection"].InnerText.ToLower() == "true")
            {
                this.MultiSection = true;
            }
            else
            {
                this.MultiSection = false;
            }
            //XmlNode xn = this.CurrentXmlNode.FirstChild;

            this.SectionList = new ACSections();
            //ACSections结点
            //this.SectionList.CurrentXmlNode = xn;
            this.SectionList.CurrentXmlNode = this.CurrentXmlNode.SelectSingleNode("ACSections");
            this.SectionList = (ACSections)this.SectionList.ConvertToObject();

            //Dimensions结点
            //xn = xn.NextSibling;
            this.DimensionList = new Dimensions();
            //this.DimensionList.CurrentXmlNode = xn;
            this.DimensionList.CurrentXmlNode = this.CurrentXmlNode.SelectSingleNode("Dimensions");
            this.DimensionList = (Dimensions)this.DimensionList.ConvertToObject();
            return this;
        }
        #endregion
        #region 重写Equals
        /// <summary>
        /// 确定指定的 AccountInfo 对象是否等效于当前 AccountInfo
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            ACMMode ai = null;
            try
            {
                ai = (ACMMode)obj;
            }
            catch
            {
                return false;
            }
            bool result = true;

            if (this.MultiSection != ai.MultiSection)
            {
                result = false;
            }
            else if (!this.SectionList.Equals(ai.SectionList))
            {
                result = false;
            }
            else if (!this.DimensionList.Equals(ai.DimensionList))
            {
                result = false;
            }
           
            return result;
        }

        #endregion
        #region 重写GetHashCode
        /// <summary>
        /// 哈希函数
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            int result = 1;
            PropertyInfo[] pi = this.GetType().GetProperties();
            foreach (PropertyInfo onofpi in pi)
            {
                result = result + onofpi.GetHashCode();
            }
            return result;
        }
        #endregion
    }
}
